package com.lgx.mall.web.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.web.Constants;
import com.lgx.common.web.util.DateUtils;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;
/**
 * 登录控制器
 * @author Administrator
 *
 */
@Controller
public class LoginController {
	
	
	@Autowired
	private IUserService userService;
	
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	/* 为登录页面加载head */
	@RequestMapping("/login/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		return "login/head";
	}
	
	/* 为登录页面加载登录页面ftl */
	@RequestMapping("/login/login.ftl")
	public String initLogin(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		return "login/login";
	}
	
	/* 跳转到登录页面  */
	@RequestMapping("/login.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "login/login";
	}
	
	/**
	 * 会员登录到后台
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/login/userLogin.htm")  
	public String userLogin(HttpServletRequest request,HttpServletResponse response)	{
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		
		String username = request.getParameter("username");
		String password = request.getParameter("passwordMD5");
		String vaildcode = request.getParameter("vaildcode");
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		if((StringUtils.isEmpty(username) || StringUtils.isEmpty(password))&& user == null)
		{
			return "login/login";
		}
		
		if(user != null)
		{
			try {
				response.sendRedirect(basePath);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "../../index";
		}
		
		/* 验证码  */
		String _code = (String) request.getSession().getAttribute(Constants.VALIDATE_CODE);
		if(!vaildcode.toLowerCase().equals(_code.toLowerCase())){
			request.setAttribute("errorMsgVaildcode", "验证码为空或输入不正确！");
			return "login/login";
		}
		
		user = userService.loginUser(username, password);
		
		if(user == null){
			request.setAttribute("errorMsgUser", "用户和密码错误或者不存在！");
			return "login/login";
		}
		
		request.getSession().setAttribute(Constants.LOGIN_USER, user);
		String currentDate = DateUtils.dateToChar(new Date(), DateUtils.DATE_FORMAT_ZH) + " " +DateUtils.getDayWeek(new Date());
		request.getSession().setAttribute("currentDate", currentDate);
		
		try {
			response.sendRedirect(basePath);    //重定向
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "../../index";
	}
	
	@RequestMapping("/logout.htm") 
	public String logout(HttpServletRequest request,HttpServletResponse response)
	{
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		request.getSession().removeAttribute(Constants.LOGIN_USER);
		try {
			response.sendRedirect(basePath);  //重定向
		} catch (IOException e) {
			e.printStackTrace();
		}    
		return "../../index";
	}
}
